iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
1

真的覺得每個能準時交功課的大大~到底是囤多少稿

當數字要顯示整數或有小數點的樣子

int x =(int)20.9

出來是20不是21因為無條件捨去

這裡會寫到字符串~

從Arrays來寫-陣列:例如聲明5個人的年齡

有兩種寫法都是可以的~

int[] userAge = { 21,22,23,24,25};

int[] userAge2;
            userAge2 = new[] { 21, 22, 23, 24, 25 };

https://ithelp.ithome.com.tw/upload/images/20211206/20119035gv0jOGJLhf.png

再來就是默認是{0,0,0,0,0}的寫法

把第一個數字0換成31的寫法:userAge3[0] = 31;

把第一個數字0換成20的寫法:userAge3[2] = userAge3[2] +20 ;

https://ithelp.ithome.com.tw/upload/images/20211206/20119035OnqwSZOkLu.png

知道裡面有多少個的語法:

using System;

namespace HelloWorld2
{
     class Program
    {
        public static void Main(string[] args)
        {
            
            int[] userAge = {21,22,23,24,25};
            Console.WriteLine(userAge.Length);
        }
    }
}

https://ithelp.ithome.com.tw/upload/images/20211208/20119035DCL65JAh6A.png


由小排到大

using System;

namespace HelloWorld2
{
     class Program
    {
        public static void Main(string[] args)
        {
            
            int[] userAge = {21,22,23,24,25};
           
            Array.Sort(userAge);
        }
    }
}


要找21是否在裡面

using System;

namespace HelloWorld2
{
     class Program
    {
        public static void Main(string[] args)
        {
            
            int[] numbers = {10,30,44,21,51,21,61,24,14};
            int ans = Array.IndexOf(numbers,21);
                   }
    }
}

找到21是第3個

當找不到數字會顯示-1

String 字符串.一定要放在""

using System;

namespace HelloWorld2
{
     class Program
    {
        public static void Main(string[] args)
        {

            string message = "Hello World" + " my name is John";
            string message2 = "Hello World, my name is John";
        }
    }
}


裡面有幾個字:

using System;

namespace HelloWorld2
{
     class Program
    {
        public static void Main(string[] args)
        {
            string message = "Hello World, ";
            int ans = message.Length;
        }
    }
}

https://ithelp.ithome.com.tw/upload/images/20211208/20119035UfHmn0voCh.png

從第幾個開始取後面的字:

using System;

namespace HelloWorld2
{
     class Program
    {
        public static void Main(string[] args)
        {
            string message = "Hello World, ";
            Console.WriteLine(message.Substring(2));
        }
    }
}

https://ithelp.ithome.com.tw/upload/images/20211208/20119035VrvB0nDk12.png

https://ithelp.ithome.com.tw/upload/images/20211208/20119035jK08usyb2M.png


查驗兩個字一樣嗎 equals

firstString.Equals("This is John "); //結果是true
firstString.Equals(secondString);//結果是false

using System;

namespace HelloWorld2
{
    class Program
   {
       public static void Main(string[] args)
       {
           string firstString = "This is John ";
           string secondString = "Hello";

           firstString.Equals("This is John ");
           firstString.Equals(secondString);
           
       }
   }
}

split分開~

using System;

namespace HelloWorld2
{
     class Program
    {
        public static void Main(string[] args)
        {
            char[] separator = { ',', ';' };
            string fruits = "Apple,Banana;Durian,,Mango";

            string[] substrings=fruits.Split(separator);
            Console.WriteLine(substrings);


        }
    }
}


/images/emoticon/emoticon05.gif

DEAR ALL 我們明天見/images/emoticon/emoticon06.gif


上一篇
關於運算符
下一篇
鏈表是甚麼?
系列文
C#可以做出甚麼?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言